{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e8d806be",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# Pooling\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1d620804",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:38.208041Z",
     "iopub.status.busy": "2023-08-18T20:14:38.207378Z",
     "iopub.status.idle": "2023-08-18T20:14:41.246234Z",
     "shell.execute_reply": "2023-08-18T20:14:41.245322Z"
    },
    "origin_pos": 3,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from d2l import torch as d2l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6200b6df",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Implement the forward propagation\n",
    "of the pooling layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b6b758a3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:41.250338Z",
     "iopub.status.busy": "2023-08-18T20:14:41.249663Z",
     "iopub.status.idle": "2023-08-18T20:14:41.255693Z",
     "shell.execute_reply": "2023-08-18T20:14:41.254862Z"
    },
    "origin_pos": 6,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "def pool2d(X, pool_size, mode='max'):\n",
    "    p_h, p_w = pool_size\n",
    "    Y = torch.zeros((X.shape[0] - p_h + 1, X.shape[1] - p_w + 1))\n",
    "    for i in range(Y.shape[0]):\n",
    "        for j in range(Y.shape[1]):\n",
    "            if mode == 'max':\n",
    "                Y[i, j] = X[i: i + p_h, j: j + p_w].max()\n",
    "            elif mode == 'avg':\n",
    "                Y[i, j] = X[i: i + p_h, j: j + p_w].mean()\n",
    "    return Y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b08443a6",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Validate the output of the two-dimensional max-pooling layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8fcb17f0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:41.259009Z",
     "iopub.status.busy": "2023-08-18T20:14:41.258472Z",
     "iopub.status.idle": "2023-08-18T20:14:41.285891Z",
     "shell.execute_reply": "2023-08-18T20:14:41.285098Z"
    },
    "origin_pos": 10,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4., 5.],\n",
       "        [7., 8.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.tensor([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])\n",
    "pool2d(X, (2, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3af0b6d",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "The average pooling layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "db997aec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:41.289296Z",
     "iopub.status.busy": "2023-08-18T20:14:41.288755Z",
     "iopub.status.idle": "2023-08-18T20:14:41.294610Z",
     "shell.execute_reply": "2023-08-18T20:14:41.293875Z"
    },
    "origin_pos": 12,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[2., 3.],\n",
       "        [5., 6.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d(X, (2, 2), 'avg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0163a39f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Padding and Stride"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "163fc8d6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:41.298256Z",
     "iopub.status.busy": "2023-08-18T20:14:41.297476Z",
     "iopub.status.idle": "2023-08-18T20:14:41.304757Z",
     "shell.execute_reply": "2023-08-18T20:14:41.303750Z"
    },
    "origin_pos": 15,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[ 0.,  1.,  2.,  3.],\n",
       "          [ 4.,  5.,  6.,  7.],\n",
       "          [ 8.,  9., 10., 11.],\n",
       "          [12., 13., 14., 15.]]]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.arange(16, dtype=torch.float32).reshape((1, 1, 4, 4))\n",
    "X"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fe0cc6f",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Deep learning frameworks default to matching pooling window sizes and stride"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bc286034",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:41.308365Z",
     "iopub.status.busy": "2023-08-18T20:14:41.307491Z",
     "iopub.status.idle": "2023-08-18T20:14:41.314528Z",
     "shell.execute_reply": "2023-08-18T20:14:41.313528Z"
    },
    "origin_pos": 19,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[10.]]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d = nn.MaxPool2d(3)\n",
    "pool2d(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "def3e95d",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "The stride and padding can be manually specified"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ca0c78a7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:41.318117Z",
     "iopub.status.busy": "2023-08-18T20:14:41.317241Z",
     "iopub.status.idle": "2023-08-18T20:14:41.324934Z",
     "shell.execute_reply": "2023-08-18T20:14:41.323915Z"
    },
    "origin_pos": 24,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[ 5.,  7.],\n",
       "          [13., 15.]]]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d = nn.MaxPool2d(3, padding=1, stride=2)\n",
    "pool2d(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "69b31fea",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:41.328395Z",
     "iopub.status.busy": "2023-08-18T20:14:41.327817Z",
     "iopub.status.idle": "2023-08-18T20:14:41.335285Z",
     "shell.execute_reply": "2023-08-18T20:14:41.334272Z"
    },
    "origin_pos": 29,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[ 5.,  7.],\n",
       "          [13., 15.]]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d = nn.MaxPool2d((2, 3), stride=(2, 3), padding=(0, 1))\n",
    "pool2d(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c16425c",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "The pooling layer pools each input channel separately"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e54e620e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:41.338784Z",
     "iopub.status.busy": "2023-08-18T20:14:41.338035Z",
     "iopub.status.idle": "2023-08-18T20:14:41.345454Z",
     "shell.execute_reply": "2023-08-18T20:14:41.344440Z"
    },
    "origin_pos": 34,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[ 0.,  1.,  2.,  3.],\n",
       "          [ 4.,  5.,  6.,  7.],\n",
       "          [ 8.,  9., 10., 11.],\n",
       "          [12., 13., 14., 15.]],\n",
       "\n",
       "         [[ 1.,  2.,  3.,  4.],\n",
       "          [ 5.,  6.,  7.,  8.],\n",
       "          [ 9., 10., 11., 12.],\n",
       "          [13., 14., 15., 16.]]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.cat((X, X + 1), 1)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "87e7f759",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T20:14:41.349138Z",
     "iopub.status.busy": "2023-08-18T20:14:41.348332Z",
     "iopub.status.idle": "2023-08-18T20:14:41.355713Z",
     "shell.execute_reply": "2023-08-18T20:14:41.354667Z"
    },
    "origin_pos": 38,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[ 5.,  7.],\n",
       "          [13., 15.]],\n",
       "\n",
       "         [[ 6.,  8.],\n",
       "          [14., 16.]]]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d = nn.MaxPool2d(3, padding=1, stride=2)\n",
    "pool2d(X)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "language_info": {
   "name": "python"
  },
  "required_libs": [],
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}